const util = require('../../../utils/util')
const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const today = util.formatDate(new Date())
// pages/export_flight_logs/export_flight_logs.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    today,
    label_id:'',
    status_id:'',
    label_list:[],
    date_start:'',
    date_end:today,
  },


  set_date_start(e){
    this.setData({
      date_start:e.detail.value,
    })
    wx.showLoading()
    this.load_count(this.data.jg_id,this.data.label_id, this.data.date_start, this.data.date_end)
    
  },

  set_date_end(e){
    this.setData({
      date_end:e.detail.value,
    })
    wx.showLoading()
    this.load_count(this.data.jg_id,this.data.label_id, this.data.date_start, this.data.date_end)

  },


  select_label(e){
    getApp().get_vibration()
    this.setData({
      label_id:e.currentTarget.id
    })
    wx.showLoading()
    this.load_count(this.data.jg_id,this.data.label_id, this.data.date_start, this.data.date_end)
  },

  
  load_count(jg_id, label_id, date_start, date_end){
    wx.request({
      url: domainName + 'export_test_rec_count',
      data: {jg_id, label_id, date_start,date_end}, ...parameter,
      success: res => {
        wx.hideLoading()
        this.setData({
          count:res.data.count
        })
      },
    })
  },


  // 确认导出
  confirm(){
    getApp().get_vibration()

    if(this.data.date_end && this.data.date_start){
      if(this.data.count>0){
        wx.showModal({
          content: '确定导出数据吗？',
          showCancel: true,
          title: '提示',
          success: res => {
            if(res.confirm){
              wx.showLoading({
                title: '大量数据计算中……',
              })
              wx.request({
                url: domainName + 'export_test_rec_export',
                data: {
                  jg_id:this.data.jg_id,
                  status_id:this.data.status_id,
                  label_id:this.data.label_id,
                  date_start:this.data.date_start,
                  date_end:this.data.date_end,
                },...parameter,
                success: res => {
                  wx.hideLoading()
                  console.log('res', res.data)
                  let file_path = res.data.file_path
                  if(file_path){
                    // callback 写法
                    wx.showLoading({
                      title: '正常下载文件（数据较大，等待较久）……',
                    })
                    wx.downloadFile({
                      url: domainName + file_path,
                      success: res => {
                        // console.log('res',res)
                        wx.hideLoading()

                        if (res.statusCode === 200) {
                          // 下载完成后转发
                          wx.shareFileMessage({
                            filePath: res.tempFilePath,
                          })
                          // 删除服务器文件
                          wx.request({
                            url: domainName + '/rm_temp_file',
                            data: {
                              file_path,
                            },...parameter,
                            success: res => {
                              console.log('删除服务器文件',res)
                            },
                          })
                        }else{
                          wx.showToast({
                            title: '文件读取失败',
                          })
                        }
                      },
                    })

                  }else{
                    wx.showToast({
                      title: '导出错误',
                      duration: 1500,
                      icon: 'error',
                      mask: false,
                    })
                  }
                },
              })
            }
          },
        })
    
      }else{
        wx.showToast({
          title: '人数为 0',
          icon: 'error',
        })
      }
  
    }else{
      wx.showToast({
        title: '请设置时段',
        icon: 'error',
      })
    }


  },

  page_init(jg_id){
    wx.request({
      url: domainName + 'export_stu_info_init',
      data: {jg_id},...parameter,
      success: res => {
        this.setData({
          label_list:res.data.label_list,
        })
      },
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.jg_id = options.jg_id
    this.page_init(this.data.jg_id)
    this.load_count(this.data.jg_id,this.data.label_id, this.data.date_start, this.data.date_end)

  },

})